package com.rinlink.jbcodec.transform;

import com.alibaba.fastjson.JSONObject;
import com.rinlink.jbcodec.JSONCodec;
import com.rinlink.jbcodec.ObjectToBytesCreatorMap;
import com.rinlink.jbcodec.common.BytesUtils;
import com.rinlink.jbcodec.exception.TransformException;

import java.util.List;

public class JSONListToBytes extends ObjectListToBytess<List<JSONObject>> {

    private ObjectToBytesCreatorMap creatorMap;
    private JSONCodec.Scheme scheme;

    public JSONListToBytes(List<JSONObject> list, JSONCodec.Scheme scheme,ObjectToBytesCreatorMap creatorMap) {
        super(list);
        this.creatorMap = creatorMap;
        this.scheme = scheme;
    }

    @Override
    public byte[] transform() throws TransformException {
        int size = getList().size();
        byte[][] result = new byte[size][];
        for (int i = 0; i < size; i++){
            JSONObject data = getList().get(i);
            JSONToBytes jsonToBytes = new JSONToBytes(data,scheme,creatorMap);
            result[i] = jsonToBytes.transform();
        }
        return BytesUtils.concat(result);
    }
}
